#include "mytcpprogress.h"

mytcpprogress::mytcpprogress(QObject *parent)
    : QObject{parent}
{
    index = -1;//初始化索引为-1,便于检查错误

    m_tcpSocket = new QTcpSocket(this);

    m_aliveTimer = new QTimer(this);
    m_aliveTimer->setInterval(500);//检查间隔为0.5s

    connect(m_aliveTimer,SIGNAL(timeout()),this,SLOT(on_timer_out()));

}


mytcpprogress::~mytcpprogress()
{


    if(m_tcpSocket->state() == QTcpSocket::ConnectedState){//如果此对象被析构时连接未断开则先断开套接字连接再析构
        m_tcpSocket->disconnectFromHost();
        m_tcpSocket->close();
    }
    if(m_tcpSocket != NULL){

        delete m_tcpSocket;
    }

    if(m_aliveTimer != NULL){
        m_aliveTimer->stop();
        delete m_aliveTimer;
    }


}

void mytcpprogress::on_newConnect_join(qintptr socket,int index)
{
    this->index = index;

    m_tcpSocket->setSocketDescriptor(socket ) ;//获取socket
    if(m_tcpSocket->state() == QTcpSocket::UnconnectedState){//如果socket获取客户端连接失败
        QMessageBox::warning(0,tr("警告"),tr("客户端连接出错."));
        emit disconnected("none",this->index);
        return;
    }

    //以下代码当套接字连接成功时执行
    connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(on_message_readyread()));
    m_peerAddress = m_tcpSocket->peerAddress().toString(); //保存对方地址


    m_aliveTimer->start();
    emit connected(m_peerAddress,this->index);//发送连接成功信号

}

void mytcpprogress::on_message_send(QByteArray sendData,int index)
{
    if(index == this->index){//判断是否为本线程发送消息
        m_tcpSocket->write(sendData);
    }
}

void mytcpprogress::on_timer_out()
{
    if(m_tcpSocket->state() == QAbstractSocket::UnconnectedState){//如果在连接期间检测到连接断开
        m_aliveTimer->stop();//停止保活计时器
        delete m_aliveTimer;
        m_aliveTimer = NULL;
        emit disconnected(m_peerAddress,this->index);//发送连接断开信号

    }
}

void mytcpprogress::on_message_readyread()
{
    QByteArray byte = m_tcpSocket->readAll();
    emit newMessage(byte,this->index);
}

void mytcpprogress::onNewTcpServiceProgress(QString address, int port, int index)
{
    this->index = index;
    connect(m_tcpSocket,SIGNAL(connected()),this,SLOT(asTcpServiceConnect()));
    m_tcpSocket->connectToHost(QHostAddress(address),port);
    if(m_tcpSocket->waitForConnected(5000) == false){
        emit disconnected("none",this->index);
    }

}

void mytcpprogress::asTcpServiceConnect()
{
    m_peerAddress = m_tcpSocket->peerAddress().toString();
    m_aliveTimer->start();
    connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(on_message_readyread()));
    emit connected(m_tcpSocket->peerAddress().toString(),this->index);

}

void mytcpprogress::onGotoDisconnect()
{


    if(m_tcpSocket->state() == QTcpSocket::ConnectedState){//如果此对象被析构时连接未断开则先断开套接字连接再析构
        m_tcpSocket->disconnectFromHost();
        m_tcpSocket->close();
    }
    if(m_tcpSocket != NULL){

        delete m_tcpSocket;
    }

    if(m_aliveTimer != NULL){
        m_aliveTimer->stop();
        delete m_aliveTimer;
    }

}

void mytcpprogress::onGotoDisconnect(int index)
{
    if(this->index == index){

        if(m_tcpSocket->state() == QTcpSocket::ConnectedState){//如果此对象被析构时连接未断开则先断开套接字连接再析构
            m_tcpSocket->disconnectFromHost();
            m_tcpSocket->close();
        }
        if(m_tcpSocket != NULL){

            delete m_tcpSocket;
        }

        if(m_aliveTimer != NULL){
            m_aliveTimer->stop();
            delete m_aliveTimer;
        }
    }


}


